package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public List<CategoryBrand> getPageList(CategoryBrand categoryBrand) {
        return baseMapper.getPageList(categoryBrand);
    }

    @Override
    public boolean saveCategoryBrand(CategoryBrand categoryBrand) {
        //根据分类id和品牌id判断数据库中是否已经添加了该分类品牌
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if (count > 0) {
            //证明已经添加国该分类品牌
            throw new ServiceException("数据库中已经添加过该分类品牌");
        }
        return baseMapper.insert(categoryBrand)>0;
    }

    @Override
    public CategoryBrand getCategoryBrandById(Long id) {
        //调用自带的根据id查询的方法
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        //创建一个集合存放父id
        List<Long> parentIds = new ArrayList<>();
        //获取当前分类品牌的分类id
        Long categoryId = categoryBrand.getCategoryId();
        //获取当前分类的父id
        List<Long> categoryIdList = getParentIds(categoryId,parentIds);
        //将集合中的元素反转
        Collections.reverse(categoryIdList);
        //将当前分类的id设置到集合中
        categoryIdList.add(categoryId);
        //给当前分类品牌设置分类id和父id
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    @Override
    public int updateCategoryBrandById(CategoryBrand categoryBrand) {
        //根据id查询数据库中的分类品牌数据
        CategoryBrand categoryBrandDatabase = baseMapper.selectById(categoryBrand.getId());
        //判断用户有没有修改分类和品牌数据
        if(categoryBrand.getBrandId().equals(categoryBrandDatabase.getBrandId()) && categoryBrand.getCategoryId().equals(categoryBrandDatabase.getCategoryId())){
            //当前分类品牌并没有修改
            return 1;
        }
        //判断当前修改之后的分类品牌数据库中是否已经存在
        //根据分类id和品牌id查询数据库
        LambdaQueryWrapper<CategoryBrand> categoryBrandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装查询条件
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId());
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        //调用带条件查询的方法
        CategoryBrand categoryBrand1 = baseMapper.selectOne(categoryBrandLambdaQueryWrapper);
        if(categoryBrand1 != null){
            //证明修改之后的分类品牌数据库中已经存在
            throw new ServiceException("该分类品牌数据库中已经存在");
        }
        //调用自带的根据id更新的方法
        int update = baseMapper.updateById(categoryBrand);
        return update;
    }

    @Override
    public List<Brand> getBrandListByCategoryId(Long categoryId) {
        return baseMapper.getBranListByCategoryId(categoryId);
    }

    //获取分类父id的方法
    public List<Long> getParentIds(Long categoryId, List<Long> parentIds) {
        //根据分类id查询当前分类
        Category category = categoryService.getById(categoryId);
        //获取当前分类的父id
        Long parentId = category.getParentId();
        if(parentId > 0){
            //将父id添加到集合中
            parentIds.add(parentId);
            //重新调用getParentIds方法
            return getParentIds(parentId,parentIds);
        }
        return parentIds;
    }
}
